package com.lsh.payment.core.strategy.query;

import com.alibaba.fastjson.JSON;
import com.lsh.payment.core.constant.BusinessConstant;
import com.lsh.payment.core.model.payenum.CallSystem;
import com.lsh.payment.core.model.payenum.PayWay;
import com.lsh.payment.core.strategy.config.WxPayConfig;
import com.lsh.payment.core.strategy.vo.wxpay.WxQueryResponse;
import com.lsh.payment.core.util.HttpClientUtils;
import com.lsh.payment.core.util.MD5Util;
import com.lsh.payment.core.util.XmlUtil;
import com.lsh.payment.core.util.pay.wxpay.WeChatCore;
import com.lsh.payment.core.util.pay.wxpay.WxSignature;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: lsh-payment
 * Created by peter on 16/11/6.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Component
public class WxQueryStrategy implements QueryStrategy<WxQueryResponse> {

    private static Logger logger = LoggerFactory.getLogger(WxQueryStrategy.class);

    @Override
    public int getStrategyNo() {
        return BusinessConstant.WECHATQUERY;
    }

    /**
     * 查询第三方支付状态
     *
     * @param paramMap 查询参数
     * @return WxQueryBasePayResponse
     */
    @Override
    public WxQueryResponse queryPayStatusByParams(Map<String, Object> paramMap) {

        WxQueryResponse wxQueryresponse;
        Map<String, String> wxQueryBody = new HashMap<>(8);
        String wxpayKey;
        Integer system = (Integer)paramMap.get("system");
        if (PayWay.ANDROID.getName().equals(paramMap.get("pay_way")) || PayWay.IOS.getName().equals(paramMap.get("pay_way"))) {
            if(system.equals(CallSystem.YG.getCode())){
                wxQueryBody.put("appid", WxPayConfig.APPID_APP);
            }else {
                wxQueryBody.put("appid", WxPayConfig.APPID_APP_TMS);
            }

            wxQueryBody.put("mch_id", WxPayConfig.MCHID_APP);
            wxpayKey = WxPayConfig.KEY_APP;
        } else if (PayWay.H5.getName().equals(paramMap.get("pay_way"))) {

            wxQueryBody.put("appid", WxPayConfig.APPID);
            wxQueryBody.put("mch_id", WxPayConfig.MCHID);
            wxpayKey = WxPayConfig.KEY;

        } else {
            return null;
        }

        wxQueryBody.put("out_trade_no", paramMap.get("out_trade_no").toString());
        wxQueryBody.put("nonce_str", MD5Util.generateNonceStr());
        wxQueryBody.put("sign", WxSignature.getNewSign(wxQueryBody, wxpayKey));
        String wxpayRequestXml = XmlUtil.toXml(wxQueryBody);
        logger.info("查询请求微信,参数:" + wxpayRequestXml);
        String wxpayResponseXml = HttpClientUtils.doPostXml(WxPayConfig.QUERY_API, wxpayRequestXml);
        JSONObject wxpayResponseJson = XmlUtil.parseXmlJson(wxpayResponseXml);
        wxQueryresponse = (WxQueryResponse) JSONObject.toBean(wxpayResponseJson, WxQueryResponse.class);
        wxQueryresponse = (WxQueryResponse) WeChatCore.mkResponse(wxpayResponseJson, wxQueryresponse, wxpayKey);
        logger.info("微信查询返回:" + JSON.toJSONString(wxQueryresponse));

        return wxQueryresponse;

    }

}
